package com.xiaoyu.lingdian.controller;

import java.util.List;
import java.util.ArrayList;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;
import javax.servlet.http.HttpServletResponse;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import org.springframework.stereotype.Controller;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.entity.CoreArea;
import com.xiaoyu.lingdian.service.CoreAreaService;
import com.xiaoyu.lingdian.vo.CoreAreaVO;

@Controller
@RequestMapping(value="/coreArea")
public class CoreAreaController extends BaseController {

	/**
	* CoreArea表
	*/
	@Autowired
	private CoreAreaService coreAreaService;

	/**
	* 添加
	*
	* @param craraName 版块名称
	* @param craraTop 所属大版块
	* @param craraOrd 排序号
	* @return
	*/
	@RequestMapping(value="/add/coreArea", method=RequestMethod.POST)
	public void addCoreArea (String craraName, String craraTop, Integer craraOrd, HttpServletResponse response) {
		logger.info("[CoreAreaController]:begin addCoreArea");
		if (StringUtil.isEmpty(craraName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[版块名称]不能为空!"), response);
			return;
		}		CoreArea coreArea = new CoreArea();
		String uuid = RandomUtil.generateString(16);
		coreArea.setCraraUuid(uuid);
		coreArea.setCraraName(craraName);
		coreArea.setCraraTop(craraTop);
		coreArea.setCraraOrd(craraOrd);
		coreAreaService.insertCoreArea(coreArea);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "新增成功!"),response);
		logger.info("[CoreAreaController]:end addCoreArea");

	}

	/**
	* 修改
	*
	* @param craraUuid 标识UUID
	* @param craraName 版块名称
	* @param craraTop 所属大版块
	* @param craraOrd 排序号
	* @return
	*/
	@RequestMapping(value="/update/coreArea", method=RequestMethod.POST)
	public void updateCoreArea (String craraUuid, String craraName, String craraTop, Integer craraOrd, HttpServletResponse response) {
		logger.info("[CoreAreaController]:begin updateCoreArea");		if (StringUtil.isEmpty(craraUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(craraName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[版块名称]不能为空!"), response);
			return;
		}		CoreArea coreArea = new CoreArea();
		coreArea.setCraraUuid(craraUuid);
		coreArea.setCraraName(craraName);
		coreArea.setCraraTop(craraTop);
		coreArea.setCraraOrd(craraOrd);
		coreAreaService.updateCoreArea(coreArea);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CoreAreaController]:end updateCoreArea");

	}

	/**
	* 删除
	*
	* @param craraUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCoreArea (String craraUuid, HttpServletResponse response) {
		logger.info("[CoreAreaController]:begin deleteCoreArea");
		if (StringUtil.isEmpty(craraUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreArea coreArea = new CoreArea();
		coreArea.setCraraUuid(craraUuid);

		coreAreaService.deleteCoreArea(coreArea);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CoreAreaController]:end deleteCoreArea");

	}

	/**
	* 批量删除
	*
	* @param craraUuids UUID集合
	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreArea (String craraUuids, HttpServletResponse response) {
		logger.info("[CoreAreaController]:begin deleteBatchCoreArea");
		if (StringUtil.isEmpty(craraUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}
		String[] uuids=craraUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreAreaService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreAreaController]:end deleteBatchCoreArea");

	}

	/**
	* 获取单个
	*
	* @param craraUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCoreArea (String craraUuid, HttpServletResponse response) {
		logger.info("[CoreAreaController]:begin viewsCoreArea");
		if (StringUtil.isEmpty(craraUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreArea coreArea = new CoreArea();
		coreArea.setCraraUuid(craraUuid);

		coreArea = coreAreaService.getCoreArea(coreArea);

		CoreAreaVO coreAreaVO = new CoreAreaVO();
		coreAreaVO.convertPOToVO(coreArea);
		if(!StringUtil.isEmpty(coreAreaVO.getCraraTop())){
			CoreArea fatherCoreArea = new CoreArea();
			fatherCoreArea.setCraraUuid(coreAreaVO.getCraraTop());
			fatherCoreArea=coreAreaService.getCoreArea(fatherCoreArea);
			if(fatherCoreArea!=null){
				coreAreaVO.setCraraFatherName(fatherCoreArea.getCraraName());
			}
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreAreaVO), response);
		logger.info("[CoreAreaController]:end viewsCoreArea");

	}

	/**
	* 获取分类列表<Page>
	* 
	* @param craraName 版块名称	
	* @param pageNum 页码
	* @param pageSize 页数
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCoreAreaPage (String craraName, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CoreAreaController]:begin findCoreAreaPage");

		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreArea> page = coreAreaService.findCoreAreaPage(craraName, pageNum, pageSize);
		Page<CoreAreaVO> pageVO = new Page<CoreAreaVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreAreaVO> vos = new ArrayList<CoreAreaVO>();
		CoreAreaVO vo;
		for (CoreArea coreArea : page.getResult()) {
			vo = new CoreAreaVO();
			vo.convertPOToVO(coreArea);
			vos.add(vo);
		}
		pageVO.setResult(vos);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO), response);
		logger.info("[CoreAreaController]:end findCoreAreaPage");

	}
	
	
	/**
	* 获取大版块下的小级版块列表<Page>
	* 
	* @param craraTop 大版块类型
	* @param pageNum 页码
	* @param pageSize 页数
	* @return
	*/
	@RequestMapping(value="/find/by/cnd/craraTop", method=RequestMethod.POST)
	public void findCoreAreaPageByCraraTop (String craraTop, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CoreAreaController]:begin findCoreAreaPageByCraraTop");		
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreArea> page = coreAreaService.findCoreAreaPageByCraraTop(craraTop, pageNum, pageSize);
		Page<CoreAreaVO> pageVO = new Page<CoreAreaVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreAreaVO> vos = new ArrayList<CoreAreaVO>();
		CoreAreaVO vo;
		for (CoreArea coreArea : page.getResult()) {
			vo = new CoreAreaVO();
			vo.convertPOToVO(coreArea);
			vos.add(vo);
		}
		pageVO.setResult(vos);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreAreaController]:end findCoreAreaPageByCraraTop");
	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}